//
//  FindHotViewController.m
//  HJZWeiBo
//
//  Created by lanou on 16/5/31.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "FindHotViewController.h"
#import "Status.h"
#import "User.h"
#import "NearListTableViewCell.h"
#import "FindHotTableViewCell.h"


@interface FindHotViewController ()<WBHttpRequestDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableDictionary *dic;
@property (nonatomic, strong) NSMutableArray *statusesArray;
@property (nonatomic, strong) NSMutableArray *userArray;
@property (nonatomic, strong) UITableView *tableView;


@end

@implementation FindHotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.statusesArray = [NSMutableArray array];
    self.userArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.view.backgroundColor = [UIColor whiteColor];
    self.dic = [NSMutableDictionary dictionary];
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    
    [self.dic setObject:token forKey:@"access_token"];
//    [self.dic setObject:@"50" forKey:@"count"];
//    [self.dic setObject:@"1" forKey:@"page"];
    
    [WBHttpRequest requestWithURL:kFindHotUrl httpMethod:@"GET" params:self.dic delegate:self withTag:@"hot"];
    
    
    [_tableView registerClass:[NearListTableViewCell class] forCellReuseIdentifier:@"NearListCell"];
    [_tableView registerClass:[FindHotTableViewCell class] forCellReuseIdentifier:@"FindHotCell"];
    // Do any additional setup after loading the view.
}

- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *statusesArray = dic[@"statuses"];
    for (NSDictionary *dic in statusesArray) {
        Status *statusModel = [[Status alloc]init];
        [statusModel setValuesForKeysWithDictionary:dic];
        [self.statusesArray addObject:statusModel];
        NSDictionary *userDic = dic[@"user"];
        User *userModel = [[User alloc]init];
        [userModel setValuesForKeysWithDictionary:userDic];
        [self.userArray addObject:userModel];
    }
    NSLog(@"%ld", self.userArray.count);
    NSLog(@"%ld", self.statusesArray.count);
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%ld", self.userArray.count);
    return self.userArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FindHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FindHotCell" forIndexPath:indexPath];
    cell.userModel = _userArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
